import router from '@system.router';

export default {
    data: {
        emotions: [
            {label: "焦虑 80-99", ratio: 0, color: "#ffa500"},
            {label: "紧张 60-79", ratio: 0, color: "#ffff00"},
            {label: "正常 30-59", ratio: 0, color: "#00ffff"},
            {label: "放松 01-29", ratio: 0, color: "#4169e1"}]
    },
    rand(min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    },
    ratios(emotions) {
        let anxiety = 0;
        let tension = 0;
        let normal  = 0;
        let relaxed = 0;

        for (let i = 0; i < emotions.length; i++)
            if (80 <= emotions[i] && emotions[i] <= 99)
                anxiety++;
            else
            if (60 <= emotions[i] && emotions[i] <= 79)
                tension++;
            else
            if (30 <= emotions[i] && emotions[i] <= 59)
                normal++;
            else
                relaxed++;

        this.emotions[0].ratio = Math.round(
            anxiety / emotions.length * 100);
        this.emotions[1].ratio = Math.round(
            tension / emotions.length * 100);
        this.emotions[2].ratio = Math.round(
            normal  / emotions.length * 100);
        this.emotions[3].ratio = Math.round(
            relaxed / emotions.length * 100);
    },
    onInit() {
        let emotions = [];
        for (let i = 0; i < 48; i++)
            emotions.push(this.rand(1, 99));
        this.ratios(emotions);
    },
    onSwipe(e) {
        switch (e.direction) {
            case "left":
                router.replace({
                    uri: "pages/index/index"
                });
                break;

            case "top":
                router.replace({
                    uri: "pages/heartrate/heartrate"
                });
                break;
        }
    }
}
